<form class="form-horizontal form-modal" method="post" enctype="multipart/form-data" id="addForm">

    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title">添加角色组</h4>
            </div>

            <div class="modal-body">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="form-group row">
                            <label class="col-sm-2 col-form-label">父级 ：</label>
                            <div class="col-sm-10">
                                <select class="form-control" name="parent_id"
                                        onchange="change_rule('{$Think.config.admin.app_host}/admin/group/rule')">
                                    {foreach $list as $item}
                                        <option value="{$item->id}">{$item->level_name|raw}</option>
                                    {/foreach}
                                </select>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>

                        <div class="form-group row">
                            <label class="col-sm-2 col-form-label">名称 ：</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="name">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>

                        {if !empty($ruleHtml)}
                        <div class="form-group row ">
                            <label class="col-sm-2 control-label">权限 ：</label>
                            <div class="col-sm-10 white-bg pt-3 pb-3">

                                <div class="row">
                                    <div class="col-sm-4">
                                        <input type="text" placeholder="请输入关键词..." class="form-control" id="search_rule">
                                    </div>
                                    <div class="col-sm-2 pull-right" style="line-height: 35px !important;">
                                        <button type="button" data-action="collapse-all" class="btn btn-success btn-xs nestable-status">
                                            <i class="fa fa-compress"></i>&nbsp;收起
                                        </button>
                                    </div>
                                </div>

                                <div id="rule" class="m-t-md col-sm-12 ">
                                    {$ruleHtml|raw}
                                </div>
                                <input type="hidden" name="rule_ids">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        {/if}

                        <div class="form-group row">
                            <label class="col-sm-2 col-form-label">状态 ：</label>
                            <div class="col-sm-10">
                                <label class="lab-radio">
                                    <input type="radio" value="1" name="status" checked="">显示
                                </label>
                                <label class="lab-radio">
                                    <input type="radio" value="0" name="status">隐藏
                                </label>
                            </div>
                        </div>

                    </div>
                </div>
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="add_rule($(this),'{$Think.config.admin.app_host}/admin/group')">保存
                </button>
            </div>
        </div>
    </div>
</form>


<script>
    $(function () {
        jsTree($('#rule'));
        $(".nestable-status").click(function () {
            let action = $(this).data("action");

            if (action === 'expand-all') {
                $('#rule').jstree().open_all();
                $(this).data("action", "collapse-all");
                $(this).html('<i class="fa fa-compress"></i>&nbsp;收起');
            } else {
                $('#rule').jstree().close_all();
                $(this).data("action", "expand-all");
                $(this).html('<i class="fa fa-expand"></i>&nbsp;展开');
            }
        });
    })

    function jsTree(obj) {
        obj.jstree({
            'core': {
                "themes": {
                    "theme": "default",
                    "dots": true,
                    "icons": false
                }
            },
            'plugins': ['types', 'checkbox', 'dnd', "search"],
        });
    }

    $("#search_rule").on("keyup", function () {
        $("#rule").jstree(true).search($(this).val())
    });

    // 更改组id 更换权限信息
    function change_rule(url) {
        let group_id = $("select[name ='parent_id'] option:selected").val();
        url = url + '/' + group_id;
        let response = post_request(url, {});
        $("#rule").jstree("destroy");
        if (response.status == 1) {
            $("#rule").html(response.data);
            jsTree($('#rule'));
        } else {
            show_tip('提示', response.msg, 'error');
        }
    }

    // 添加权限组操作提交
    function add_rule(that, url) {
        // 获取阶段列表
        let select_list = $('#rule').jstree().get_selected(true);
        let rule_ids = '';
        $.each(select_list, function ($index, $value) {
            if (!isNaN($value.data.id)) {
                rule_ids += "," + $value.data.id;
            }
        });
        rule_ids = rule_ids.substring(1);
        $('input[name="rule_ids"]').val(rule_ids);

        post_form(that, url);
    }
</script>